@tool
extends Node3D

const COMMON_PRESETS = preload("res://scripts/procedural_buildings/presets/common_preset_bk.gd")
const I_335_PRESETS = preload("res://scripts/procedural_buildings/presets/1_335_presets_bk.gd")

@export_tool_button("Generate building") var btn_generate = generate_building
@export_tool_button("Clear") var btn_clear = clear
@export_tool_button("Save building data") var btn_save = save_building_config

@onready var procedural_block: Node3D = $"."

@export var project: BUILDING_PROJECT = BUILDING_PROJECT.P_1_335

@export var num_base_layers: int = 1
@export var num_floors: int = 5
@export var num_sections: int = 4

enum BUILDING_PROJECT {
	P_1_335
}

enum PART_POSITION {
	root,
	straight,
	left,
	right
}

const PART_ROTATION_Y: Dictionary[PART_POSITION, int] = {
	PART_POSITION.root: 0,
	PART_POSITION.straight: 0,
	PART_POSITION.left: 90,
	PART_POSITION.right: -90
}

enum PART_GROUP {
	external_panel,
	internal_sections,
	inset
}

enum PANEL_TYPE {
	panel_26,
	panel_26_base,
	panel_26_base_entrance,
	panel_26_base_vent,
	panel_26_entrance,
	panel_26_ladder_window,
	panel_26_roof,
	panel_26_window,
	panel_31,
	panel_31_base,
	panel_31_base_sealed_begin,
	panel_31_base_sealed_end,
	panel_31_base_vent,
	panel_31_roof,
	panel_31_roof_sealed_begin,
	panel_31_roof_sealed_end,
	panel_31_sealed_begin,
	panel_31_sealed_end,
	panel_31_window_left,
	panel_31_window_left_sealed_begin,
	panel_31_window_left_sealed_end,
	panel_31_window_right,
	panel_31_window_right_sealed_begin,
	panel_31_window_right_sealed_end,
	panel_32,
	panel_32_balcony,
	panel_32_base,
	panel_32_base_vent,
	panel_32_roof,
	panel_32_window,
}

enum INSET_TYPE {
	panel_26_base_entrance_panel
}

enum INTERNAL_TYPE {
	staircase_section,
	staircase_walls_base,
	staircase_walls_entrance,
	staircase_entrance_floor,
	staircase_walls_window,
	flat_walls_end_l_1,
	flat_walls_end_l_2,
	flat_walls_end_l_3,
	flat_walls_end_l_4,
	flat_walls_end_l_1_balc,
	flat_walls_end_l_2_balc,
	flat_walls_end_l_3_balc,
	flat_walls_end_l_4_balc,
	flat_walls_end_r_1,
	flat_walls_end_r_2,
	flat_walls_end_r_3,
	flat_walls_end_r_4,
	flat_walls_end_r_1_balc,
	flat_walls_end_r_2_balc,
	flat_walls_end_r_3_balc,
	flat_walls_end_r_4_balc,
	flat_walls_mid_1,
	flat_walls_mid_2,
	flat_walls_mid_3,
	flat_walls_mid_4,
	flat_walls_mid_1_balc,
	flat_walls_mid_2_balc,
	flat_walls_mid_3_balc,
	flat_walls_mid_4_balc,
	
	flat_walls_end_l_1_roof,
	flat_walls_end_l_2_roof,
	flat_walls_end_l_3_roof,
	flat_walls_end_l_4_roof,
	flat_walls_end_r_1_roof,
	flat_walls_end_r_2_roof,
	flat_walls_end_r_3_roof,
	flat_walls_end_r_4_roof,
	flat_walls_mid_1_roof,
	flat_walls_mid_2_roof,
	flat_walls_mid_3_roof,
	flat_walls_mid_4_roof,
}

const EXTERNAL_PANELS: Dictionary[PANEL_TYPE, PackedScene] = {
	PANEL_TYPE.panel_26 : preload("res://scenes/building_prefabs/panels/panel_26.tscn"),
	PANEL_TYPE.panel_26_base : preload("res://scenes/building_prefabs/panels/panel_26_base.tscn"),
	PANEL_TYPE.panel_26_base_entrance : preload("res://scenes/building_prefabs/panels/panel_26_base_entrance.tscn"),
	PANEL_TYPE.panel_26_base_vent : preload("res://scenes/building_prefabs/panels/panel_26_base_vent.tscn"),
	PANEL_TYPE.panel_26_entrance : preload("res://scenes/building_prefabs/panels/panel_26_entrance.tscn"),
	PANEL_TYPE.panel_26_ladder_window : preload("res://scenes/building_prefabs/panels/panel_26_ladder_window.tscn"),
	PANEL_TYPE.panel_26_roof : preload("res://scenes/building_prefabs/panels/panel_26_roof.tscn"),
	PANEL_TYPE.panel_26_window : preload("res://scenes/building_prefabs/panels/panel_26_window.tscn"),
	PANEL_TYPE.panel_31 : preload("res://scenes/building_prefabs/panels/panel_31.tscn"),
	PANEL_TYPE.panel_31_base : preload("res://scenes/building_prefabs/panels/panel_31_base.tscn"),
	PANEL_TYPE.panel_31_base_sealed_begin : preload("res://scenes/building_prefabs/panels/panel_31_base_sealed_begin.tscn"),
	PANEL_TYPE.panel_31_base_sealed_end : preload("res://scenes/building_prefabs/panels/panel_31_base_sealed_end.tscn"),
	PANEL_TYPE.panel_31_base_vent : preload("res://scenes/building_prefabs/panels/panel_31_base_vent.tscn"),
	PANEL_TYPE.panel_31_roof : preload("res://scenes/building_prefabs/panels/panel_31_roof.tscn"),
	PANEL_TYPE.panel_31_roof_sealed_begin : preload("res://scenes/building_prefabs/panels/panel_31_roof_sealed_begin.tscn"),
	PANEL_TYPE.panel_31_roof_sealed_end : preload("res://scenes/building_prefabs/panels/panel_31_roof_sealed_end.tscn"),
	PANEL_TYPE.panel_31_sealed_begin : preload("res://scenes/building_prefabs/panels/panel_31_sealed_begin.tscn"),
	PANEL_TYPE.panel_31_sealed_end : preload("res://scenes/building_prefabs/panels/panel_31_sealed_end.tscn"),
	PANEL_TYPE.panel_31_window_left : preload("res://scenes/building_prefabs/panels/panel_31_window_left.tscn"),
	PANEL_TYPE.panel_31_window_left_sealed_begin : preload("res://scenes/building_prefabs/panels/panel_31_window_left_sealed_begin.tscn"),
	PANEL_TYPE.panel_31_window_left_sealed_end : preload("res://scenes/building_prefabs/panels/panel_31_window_left_sealed_end.tscn"),
	PANEL_TYPE.panel_31_window_right : preload("res://scenes/building_prefabs/panels/panel_31_window_right.tscn"),
	PANEL_TYPE.panel_31_window_right_sealed_begin : preload("res://scenes/building_prefabs/panels/panel_31_window_right_sealed_begin.tscn"),
	PANEL_TYPE.panel_31_window_right_sealed_end : preload("res://scenes/building_prefabs/panels/panel_31_window_right_sealed_end.tscn"),
	PANEL_TYPE.panel_32 : preload("res://scenes/building_prefabs/panels/panel_32.tscn"),
	PANEL_TYPE.panel_32_balcony : preload("res://scenes/building_prefabs/panels/panel_32_balcony.tscn"),
	PANEL_TYPE.panel_32_base : preload("res://scenes/building_prefabs/panels/panel_32_base.tscn"),
	PANEL_TYPE.panel_32_base_vent : preload("res://scenes/building_prefabs/panels/panel_32_base_vent.tscn"),
	PANEL_TYPE.panel_32_roof : preload("res://scenes/building_prefabs/panels/panel_32_roof.tscn"),
	PANEL_TYPE.panel_32_window : preload("res://scenes/building_prefabs/panels/panel_32_window.tscn"),
}

const INSETS: Dictionary[PANEL_TYPE, PackedScene] = {
	
}

const INTERNALS: Dictionary[INTERNAL_TYPE, PackedScene] = {
	INTERNAL_TYPE.staircase_section: preload("res://scenes/building_prefabs/internals/1_335/staircase_section.tscn"),
	INTERNAL_TYPE.staircase_walls_base: preload("res://scenes/building_prefabs/internals/1_335/staircase_walls_base.tscn"),	
	INTERNAL_TYPE.staircase_walls_entrance: preload('res://scenes/building_prefabs/internals/1_335/staircase_walls_entrance.tscn'),
	INTERNAL_TYPE.staircase_entrance_floor: preload('res://scenes/building_prefabs/internals/1_335/staircase_entrance_floor.tscn'),
	INTERNAL_TYPE.staircase_walls_window: preload('res://scenes/building_prefabs/internals/1_335/staircase_walls_window.tscn'),
	
	INTERNAL_TYPE.flat_walls_end_l_1: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_1.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_2: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_2.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_3: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_3.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_4: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_4.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_1_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_1_balc.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_2_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_2_balc.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_3_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_3_balc.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_4_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_4_balc.tscn'),
	
	INTERNAL_TYPE.flat_walls_end_r_1: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_1.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_2: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_2.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_3: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_3.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_4: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_4.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_1_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_1_balc.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_2_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_2_balc.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_3_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_3_balc.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_4_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_4_balc.tscn'),
	
	INTERNAL_TYPE.flat_walls_mid_1: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_1.tscn'),
	INTERNAL_TYPE.flat_walls_mid_2: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_2.tscn'),
	INTERNAL_TYPE.flat_walls_mid_3: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_3.tscn'),
	INTERNAL_TYPE.flat_walls_mid_4: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_4.tscn'),
	INTERNAL_TYPE.flat_walls_mid_1_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_1_balc.tscn'),
	INTERNAL_TYPE.flat_walls_mid_2_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_2_balc.tscn'),
	INTERNAL_TYPE.flat_walls_mid_3_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_3_balc.tscn'),
	INTERNAL_TYPE.flat_walls_mid_4_balc: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_4_balc.tscn'),
	
	INTERNAL_TYPE.flat_walls_end_l_1_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_1_roof.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_2_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_2_roof.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_3_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_3_roof.tscn'),
	INTERNAL_TYPE.flat_walls_end_l_4_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_l_4_roof.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_1_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_1_roof.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_2_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_2_roof.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_3_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_3_roof.tscn'),
	INTERNAL_TYPE.flat_walls_end_r_4_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_end_r_4_roof.tscn'),
	INTERNAL_TYPE.flat_walls_mid_1_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_1_roof.tscn'),
	INTERNAL_TYPE.flat_walls_mid_2_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_2_roof.tscn'),
	INTERNAL_TYPE.flat_walls_mid_3_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_3_roof.tscn'),
	INTERNAL_TYPE.flat_walls_mid_4_roof: preload('res://scenes/building_prefabs/internals/1_335/flat_walls_mid_4_roof.tscn'),
}

const ANCHOR_BY_POSITION: Dictionary[PART_POSITION, String] = {
	PART_POSITION.straight: 'anchor_straight',
	PART_POSITION.left: 'anchor_corner_left',
	PART_POSITION.right: 'anchor_corner_right'
}

const i_335_preset_side_section_preset_l_base = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.root
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_entrance,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_entrance_floor,
					'anchor_to': 'anchor_staircase_base'
				},
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_base,
					'anchor_to': 'anchor_staircase_base'
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		# side wall
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_base_sealed_begin,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_base_sealed_end,
			'position': PART_POSITION.straight
		},
	]
}
const i_335_preset_side_section_preset_l = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.root
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_entrance,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_entrance,
					'anchor_to': 'anchor_staircase_walls',
					'children': [
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.staircase_section,
							'anchor_to': 'anchor_staircase'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_1,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_2,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_3,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_4,
							'anchor_to': 'anchor_flat_4'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_1_roof,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_2_roof,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_3_roof,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_4_roof,
							'anchor_to': 'anchor_flat_4'
						}
					]
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		# side wall
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_left_sealed_begin,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_right_sealed_end,
			'position': PART_POSITION.straight
		},
	]
}
const i_335_preset_side_section_preset_l_balc = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.root
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_ladder_window,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_window,
					'anchor_to': 'anchor_staircase_walls',
					'children': [
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.staircase_section,
							'anchor_to': 'anchor_staircase'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_1_balc,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_2_balc,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_3_balc,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_4_balc,
							'anchor_to': 'anchor_flat_4'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_1_roof,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_2_roof,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_3_roof,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_l_4_roof,
							'anchor_to': 'anchor_flat_4'
						}
					]
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		# side wall
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_left_sealed_begin,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_right_sealed_end,
			'position': PART_POSITION.straight
		},
	]
}

const i_335_preset_side_section_preset_r_base = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_entrance,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_entrance_floor,
					'anchor_to': 'anchor_staircase_base'
				},
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_base,
					'anchor_to': 'anchor_staircase_base'
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		# side wall
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_base_sealed_begin,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_base_sealed_end,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
	]
}
const i_335_preset_side_section_preset_r = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_entrance,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_entrance,
					'anchor_to': 'anchor_staircase_walls',
					'children': [
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.staircase_section,
							'anchor_to': 'anchor_staircase'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_1,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_2,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_3,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_4,
							'anchor_to': 'anchor_flat_4'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_1_roof,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_2_roof,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_3_roof,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_4_roof,
							'anchor_to': 'anchor_flat_4'
						},
					]
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		# side wall
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_left_sealed_begin,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_right_sealed_end,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	]
}
const i_335_preset_side_section_preset_r_balc = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_ladder_window,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_window,
					'anchor_to': 'anchor_staircase_walls',
					'children': [
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.staircase_section,
							'anchor_to': 'anchor_staircase'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_1_balc,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_2_balc,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_3_balc,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_4_balc,
							'anchor_to': 'anchor_flat_4'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_1_roof,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_2_roof,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_3_roof,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_end_r_4_roof,
							'anchor_to': 'anchor_flat_4'
						},
					]
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		# side wall
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_left_sealed_begin,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_31_window_right_sealed_end,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.left
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	]
}

const i_335_preset_medium_section_preset_base = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_entrance,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_entrance_floor,
					'anchor_to': 'anchor_staircase_base'
				},
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_base,
					'anchor_to': 'anchor_staircase_base'
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_base_vent,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_base,
			'position': PART_POSITION.straight
		},
	]
}
const i_335_preset_medium_section_preset = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_entrance,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_entrance,
					'anchor_to': 'anchor_staircase_walls',
					'children': [
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.staircase_section,
							'anchor_to': 'anchor_staircase'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_1,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_2,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_3,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_4,
							'anchor_to': 'anchor_flat_4'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_1_roof,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_2_roof,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_3_roof,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_4_roof,
							'anchor_to': 'anchor_flat_4'
						}
					]
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	]
}
const i_335_preset_medium_section_preset_balc = {
	'forward_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_ladder_window,
			'position': PART_POSITION.straight,
			'children': [
				{
					'part_group': PART_GROUP.internal_sections,
					'part_type': INTERNAL_TYPE.staircase_walls_window,
					'anchor_to': 'anchor_staircase_walls',
					'children': [
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.staircase_section,
							'anchor_to': 'anchor_staircase'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_1_balc,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_2_balc,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_3_balc,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_4_balc,
							'anchor_to': 'anchor_flat_4'
						},
						
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_1_roof,
							'anchor_to': 'anchor_flat_1'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_2_roof,
							'anchor_to': 'anchor_flat_2'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_3_roof,
							'anchor_to': 'anchor_flat_3'
						},
						{
							'part_group': PART_GROUP.internal_sections,
							'part_type': INTERNAL_TYPE.flat_walls_mid_4_roof,
							'anchor_to': 'anchor_flat_4'
						}
					]
				}
			]
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	],
	'back_pass': [
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_32_balcony,
			'position': PART_POSITION.straight
		},
		{
			'part_group': PART_GROUP.external_panel,
			'part_type': PANEL_TYPE.panel_26_window,
			'position': PART_POSITION.straight
		},
	]
}

const i_335_preset = {
	'base': {
		'left': i_335_preset_side_section_preset_l_base,
		'mid': i_335_preset_medium_section_preset_base,
		'right': i_335_preset_side_section_preset_r_base
	}
}

var levels: Array[Node3D] = []

func generate_building():
	var previous_instance: Node3D
	var previous_root: Node3D
	#var level_roots: Array[Node3D] = []
	
	for base_layer in range(num_base_layers):
		var back_pass = []
		for section_num in range(num_sections):
			var section_preset
			if section_num == 0:
				section_preset = i_335_preset_side_section_preset_l_base
			elif section_num == num_sections - 1:
				section_preset = i_335_preset_side_section_preset_r_base
			else:
				section_preset = i_335_preset_medium_section_preset_base
			for part in section_preset['forward_pass']:
				var part_instance: Node3D = add_part(part, previous_instance, previous_root)
				if part['position'] == PART_POSITION.root:
					previous_root = part_instance
				previous_instance = part_instance
			back_pass.push_front(section_preset['back_pass'])
		
		for pass_array in back_pass:
			for part in pass_array:
				var part_instance: Node3D = add_part(part, previous_instance, previous_root)
				if part['position'] == PART_POSITION.root:
					previous_root = part_instance
				previous_instance = part_instance
	
	for floor_num in range(num_floors):
		var back_pass = []
		for section_num in range(num_sections):
			var section_preset
			if section_num == 0:
				if floor_num == 0:
					section_preset = i_335_preset_side_section_preset_l
				else:
					section_preset = i_335_preset_side_section_preset_l_balc
			elif section_num == num_sections - 1:
				if floor_num == 0:
					section_preset = i_335_preset_side_section_preset_r
				else:
					section_preset = i_335_preset_side_section_preset_r_balc
			else:
				if floor_num == 0:
					section_preset = i_335_preset_medium_section_preset
				else:
					section_preset = i_335_preset_medium_section_preset_balc
			for part in section_preset['forward_pass']:
				var part_instance: Node3D = add_part(part, previous_instance, previous_root)
				if part['position'] == PART_POSITION.root:
					previous_root = part_instance
				previous_instance = part_instance
			back_pass.push_front(section_preset['back_pass'])
		for pass_array in back_pass:
			for part in pass_array:
				var part_instance: Node3D = add_part(part, previous_instance, previous_root)
				if part['position'] == PART_POSITION.root:
					previous_root = part_instance
				previous_instance = part_instance


func add_part(part, previous_instance, previous_root=null, parent=procedural_block) -> Node3D:
	var part_to_instance
	if part['part_group'] == PART_GROUP.external_panel:
		part_to_instance = EXTERNAL_PANELS
	if part['part_group'] == PART_GROUP.internal_sections:
		part_to_instance = INTERNALS
	if part['part_group'] == PART_GROUP.inset:
		part_to_instance = INSETS
	var part_instance: Node3D = part_to_instance[part['part_type']].instantiate()
	parent.add_child(part_instance)
	part_instance.owner = get_tree().edited_scene_root
	if part.has('position') and part.position == PART_POSITION.root:
		part_instance.position = Vector3.ZERO
		if previous_root != null:
			part_instance.global_position = previous_root.get_node('anchor_top').global_position
	else:
		var anchor_name
		if !part.has('anchor_to'):
			anchor_name = ANCHOR_BY_POSITION[part.position]
			part_instance.global_position = previous_instance.get_node(anchor_name).global_position
			part_instance.rotation.y += previous_instance.rotation.y + deg_to_rad(PART_ROTATION_Y[part['position']])
		else:
			anchor_name = part['anchor_to']
			part_instance.global_position = previous_instance.get_node(anchor_name).global_position
			
	if part.has('children'):
		print('Add child')
		for child in part['children']:
			add_part(child, part_instance)
	return part_instance

func clear():
	for child in procedural_block.get_children():
		child.queue_free()

func save_building_config():
	var path1 = 'insets/balcony_window_walls_sealed'
	var path2 = ''
	load("res://scenes/building_prefabs/%s.tscn" % path1)
	print('Saved?')
